package com.cos.m3.web.controller;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;

/**
 * @author: COS
 * @time: 2022/6/9 9:18
 * @description:
 */
@Component
public class MyException implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request,
                                         HttpServletResponse response,
                                         Object handler,
                                         Exception ex) {
        ex.printStackTrace();
        ModelAndView mv  = new ModelAndView();
        //判断异常类型
        if (ex instanceof NullPointerException) {
            mv.setViewName("redirect:/error.jsp");
        }else if(ex instanceof FileNotFoundException){
            mv.setViewName("redirect:/error404.jsp");
        }else{
            mv.setViewName("redirect:/error500.jsp");
        }
        return mv;
    }
}
